<?php
	require_once("../include/functions.php");
?>
<html>
  <HEAD>
    <TITLE>JoshHughes.com NFL Football Pool Site</TITLE>
    <link rel="stylesheet" href="css/NFLPool.css" type="text/css">
</head>
<body leftmargin="0" topmargin="0" bottommargin="0" rightmargin="0" marginwidth="0" marginheight="0">
<?
	//Connect To Database
	dbConnect();
	$sentToNum = 0;
	$sentTo;
	$MailFrom = "From: Football@joshhughes.com";
	//Find games starting within the next 3 Day
	$SQL = "SELECT Week, GameDate, To_DAYS(GameDate) - TO_DAYS(NOW()) FROM NFLSchedule WHERE ((To_DAYS(GameDate) - TO_DAYS(NOW())) < 4) AND ((To_DAYS(GameDate) - TO_DAYS(NOW())) > 0)";
	//$result = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
	//Open Record Set using SQL Query
	$result = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
	if($myrow = mysql_fetch_array($result)) {
		//Set Current Week For Later Query
		$CurrWeek = $myrow["Week"];
		//Set GameDate For Later Query
		$GameDate = date("Y-m-d",strtotime($myrow["GameDate"]));
		//Get Number of Games this week
		//Get Current Week
		$SQL = "SELECT * FROM NFLSchedule Where Week=".$CurrWeek;
		//Open Record Set using SQL Query
		$Gamesresult = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
		//get number of records returned by SQL
		$num_games = mysql_num_rows($Gamesresult);
		echo "# Games for week <b>$CurrWeek</b> = $num_games<br/>";
		//Select user information from the Users Table, if the user wants to be reminded
		//Write SQL Query to extract specified data
		if($CurrWeek > 17)
		{
			$SQL = "SELECT ID, username, email FROM NFLUsers WHERE PlayOffs = 1 AND RemindMe  > 0 AND flag = 1 ORDER BY username";
		}
		else
		{
			$SQL = "SELECT ID, username, email FROM NFLUsers WHERE RemindMe > 0 AND flag = 1 ORDER BY username";
		}
		//Open Record Set using SQL Query
		$Emailresult = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
		do{
		//if the user's ID is not in the NFLPicks table, they have not picked
		//and therefore thier email needs to be added to the list to be reminded.
		    if ($Emailrow["email"] != "")
		    {
			$SendMail = false;
			//set up some email varibables
			$MailSubject = "Don't forget to make picks for Week $CurrWeek";
			$MailBody = $Emailrow["username"].", don't forget to go to http://football.joshhughes.com and make your picks for the following games:\n";
			$HTMLMailBody = "<B><I>".$Emailrow["username"]."</I></B>, don't forget to go to <a href=\"http://football.joshhughes.com\">http://football.joshhughes.com</a> and make your picks for the following games:<BR>";
			$adminmail = $Emailrow["username"]." <".$Emailrow["email"].">";
			$currentDate = 20031022;
			//SQL to check and see if user made his picks
			$SQL = "SELECT * FROM NFLPicks Where (UserID = ".$Emailrow["ID"].") AND (Week = ".$CurrWeek.")";
			//Open Record Set using SQL Query
			$IDresult = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
			//get number of records returned by SQL
			$num_rows = mysql_num_rows($IDresult);
			if($num_rows == 0)
			{	//If user has not picked.
				//Get Schedule INFO and build Mail body
				$SQL = "SELECT Week, GameNum, GameDate, GameTime, Away, Home FROM NFLSchedule WHERE (Week = $CurrWeek) ORDER BY GameDate, GameTime";
				//Open Record Set using SQL Query
				$result = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
				//$currentDate = $myrow["GameDate"];
				do{
					if (($myrow["Away"] != Null) || ($myrow["Away"] != ""))
					{
					   if($myrow["GameDate"] == $CurrentDate)
					   {
						$MailBody .= "\n".$myrow["Away"]." @ ".$myrow["Home"]." - ".date('g:i A',strtotime($myrow["GameTime"]))." EST";
						$HTMLMailBody .= "<BR><img src=\"http://football.joshhughes.com/images/small/".$myrow["Away"].".gif\"> @ <img src=\"http://football.joshhughes.com/images/small/".$myrow["Home"].".gif\"> - ".date('g:i A',strtotime($myrow["GameTime"]))." EST";
					   }
					   else
					   {
						$MailBody .= "\n".date('l F jS, Y',strtotime($myrow["GameDate"]))."\n".$myrow["Away"]." @ ".$myrow["Home"]." - ".date('g:i A',strtotime($myrow["GameTime"]))." EST";
						$HTMLMailBody .= "<BR><B>".date('l F jS, Y',strtotime($myrow["GameDate"]))."</B><BR><img src=\"http://football.joshhughes.com/images/small/".$myrow["Away"].".gif\"> @ <img src=\"http://football.joshhughes.com/images/small/".$myrow["Home"].".gif\"> - ".date('g:i A',strtotime($myrow["GameTime"]))." EST";
						$CurrentDate = $myrow["GameDate"];
					   }//end if
					}//end if($myrow["Away"]...
				}while($myrow = mysql_fetch_array($result));
				$SendMail = true;
			}
			else
			{ //If user has picked, check to see if user has picked for all games.
			$UserPickedRow = mysql_fetch_array($IDresult);
			   for ($i = 1; $i <= $num_games; $i++)
			   {
				if ($UserPickedRow["Game".$i] == "")
				{
				    //Get Schedule INFO and build Mail body
				    $SQL = "SELECT Week, GameNum, GameDate, GameTime, Away, Home FROM NFLSchedule WHERE (Week = ".$CurrWeek.") AND (GameNum = ".$i.")";
				    //Open Record Set using SQL Query
				    $result = mysql_query($SQL) or die("Cannot query the database.<br>" . mysql_error());
				    do{
					if (($myrow["Away"] != Null) || ($myrow["Away"] != ""))
					{
					   if($myrow["GameDate"] == $CurrentDate)
					   {
						$MailBody .= "\n".$myrow["Away"]." @ ".$myrow["Home"]." - ".date('g:i A',strtotime($myrow["GameTime"]))." EST";
						$HTMLMailBody .= "<BR/><img src=\"http://football.joshhughes.com/images/small/".$myrow["Away"].".gif\"> @ <img src=\"http://football.joshhughes.com/images/small/".$myrow["Home"].".gif\"> - ".date('g:i A',strtotime($myrow["GameTime"]))." EST";
					   }
					   else
					   {
						$MailBody .= "\n".date('l F jS, Y',strtotime($myrow["GameDate"]))."\n".$myrow["Away"]." @ ".$myrow["Home"]." - ".date('g:i A',strtotime($myrow["GameTime"]))." EST";
						$HTMLMailBody .= "<BR/><B>".date('l F jS, Y',strtotime($myrow["GameDate"]))."</B><BR><img src=\"http://football.joshhughes.com/images/small/".$myrow["Away"].".gif\"> @ <img src=\"http://football.joshhughes.com/images/small/".$myrow["Home"].".gif\"> - ".date('g:i A',strtotime($myrow["GameTime"]))." EST";
						$CurrentDate = $myrow["GameDate"];
					   }//end if
					}//end if($myrow["Away"]...
				    }while($myrow = mysql_fetch_array($result));
				    $SendMail = true;
				}//end if ($UserPickedRow["Game".$i] == "")
			   }//end FOR LOOP
			}//end if($num_rows == 0)
			//$MailBody .= "\n\nNOTE:  The Patriots @ Bills game originally scheduled for 1:00 PM EST was moved back to 8:15 PM EST, the Bears @ Seahawks game originally scheduled for 8:15 PM EST was moved forward to 4:15 PM EST.";
			$MailBody .= "\n\n\n Users may \"Opt Out\" of this reminder feature by changing their preferences using the \"Change Email Prefs\" link in the Log On Box....\nThis feature is turned on by default at user registration.";
			$HTMLMailBody .= "<BR/><BR/><BR/><font size=1> Users may &quot;Opt Out&quot; of this reminder feature by changing their preferences using the &quot;Change Email Prefs&quot; link in the Log On Box....<br/>This feature is turned on by default at user registration.</font>";
		    }//end if ($Emailrow["email"] != "")
			if($SendMail)
			{
			  $MailTo = $adminmail;
			  /*
			  //add From: header
			  $headers = "From: Football@joshhughes.com\r\n";

			  //specify MIME version 1.0
			  $headers .= "MIME-Version: 1.0\r\n";

			  //unique boundary
			  $boundary = uniqid("HTMLDEMO");

			  //tell e-mail client this e-mail contains//alternate versions
			  $headers .= "Content-Type: multipart/alternative" ."; boundary = $boundary\r\n\r\n";

			  //message to people with clients who don't
			  //understand MIME
			  $headers .= "This is a MIME encoded message.\r\n\r\n";

			  //plain text version of message
			  $headers .= "--$boundary\r\n" .
			     "Content-Type: text/plain; charset=ISO-8859-1\r\n" .
			     "Content-Transfer-Encoding: base64\r\n\r\n";
			  $headers .= chunk_split(base64_encode($MailBody));

			  //HTML version of message
			  $headers .= "--$boundary\r\n" .
			     "Content-Type: text/html; charset=ISO-8859-1\r\n" .
			     "Content-Transfer-Encoding: base64\r\n\r\n";
			  $headers .= chunk_split(base64_encode($HTMLMailBody));
			  */
			  //Send an Email notifying user of games to pick.
			  #mail($MailTo, $MailSubject, "", $headers);  //message send
			  mail($MailTo, $MailSubject, $MailBody, $MailFrom);  //message send
			  echo "<H1>Mail Sent To: <i style=\"color: #FF0000;\">$MailTo</i></H1>";
			  $sentToNum++;
			  $sentTo[$sentToNum] = $MailTo;
			}//end if($SendMail)
		   $Sendmail = false;
		}while($Emailrow = mysql_fetch_array($Emailresult));
	}//end if($myrow = mysql_fetch_array($result))
	
	
	
	
	if($sentToNum > 0)
	{
		$MailTo = "<Football@joshhughes.com>";
		$MailFrom = "Football@joshhughes.com";
		$MailSubject = "Football Pool Daily Email Script";
		$MailBody = "The email.php script was run on ".date('l F jS, Y')." at ".date('g:i A',time() + (3600 * 3)).".\n\nMail sent to $sentToNum people:\n\n";
		foreach ($sentTo as $value)
		{
		   $MailBody.= $value."\n";
		}
		$MailBody.= "\n";
		//Send an Email to me notifying me of a request
		mail($MailTo, $MailSubject, $MailBody, $MailFrom);  //message send
		echo "<H1>$MailBody</H1>";
	}
	else
	{
		echo "The script was run on ".date('l F jS, Y')." at ".date('g:i A',time() + (3600 * 3)).".<br/><br/>Email not sent to any users";
	}
	
?>
</body>
</html>
